package com.example.split;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * Created with IntelliJ IDEA.
 * ClassName: WordCountStream
 * Package: com.example.wordcount
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-17
 * Time: 10:52
 */


//分流
public class SplitByFilterDemo {
    public static void main(String[] args) throws Exception {
        //1.创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.读取数据 从socket读
        DataStreamSource<String> data = env.socketTextStream("hadoop102", 7777);

        //1.使用filter来实现 过滤 一次只能实现一条流 但是可以写两次实现
        //缺点 同一个数据处理两遍
        SingleOutputStreamOperator<String> filter = data.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String s) throws Exception {
                return Integer.parseInt(s) % 2 == 0;
            }
        });

        SingleOutputStreamOperator<String> filter1 =
                data.filter(val -> Integer.parseInt(val) % 2 == 1);

        filter.print("偶数");
        filter1.print("奇数");

        env.execute();
    }
}
